﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Chisel.Core
{
    public class RubberBand : UserControl
    {
        /// <summary>
        /// Creates an infinitely small rubber band at the given point.
        /// </summary>
        public RubberBand(DiagramCanvas canvas, Point anchor)
            : base()
        {
            this.canvas = canvas;

            Content = rectangle;
            var stroke = new SolidColorBrush(Colors.Blue);
            rectangle.Stroke = stroke;
            var fill = new SolidColorBrush(Colors.LightGray);
            rectangle.Fill = fill;
            Opacity = 0.3;

            _anchor = anchor;
            Stop = anchor;

            canvas.Children.Add(this);
        }

        private DiagramCanvas canvas;

        private Rectangle rectangle = new Rectangle();

        /// <summary>
        /// The upper left corner of the rectangle.
        /// </summary>
        public Point UpperLeft
        {
            get { return new Point(Canvas.GetLeft(this), Canvas.GetTop(this)); }
        }

        private Point _anchor = new Point();
        /// <summary>
        /// The first point (anchor) of the rubber band.
        /// </summary>
        public Point Anchor
        {
            get { return _anchor; }
            set
            {
                _anchor = value;
                PositionRectangle();
            }
        }

        private Point _stop = new Point();
        /// <summary>
        /// The second point (stop) of the rubber band.
        /// </summary>
        public Point Stop
        {
            get {return _stop;}
            set
            {
                _stop = value;
                PositionRectangle();
            }
        }

        /// <summary>
        /// Repositions the rectangle based on the anchor and stop.
        /// </summary>
        private void PositionRectangle()
        {
            var minX = _anchor.X;
            double width = 0;
            if (_stop.X < minX)
            {
                width = minX - _stop.X;
                minX = _stop.X;
            }
            else
                width = _stop.X - minX;
            var minY = _anchor.Y;
            double height = 0;
            if (_stop.Y < minY)
            {
                height = minY - _stop.Y;
                minY = _stop.Y;
            }
            else
                height = _stop.Y - minY;

            Canvas.SetLeft(this, minX);
            Canvas.SetTop(this, minY);
            Width = width;
            Height = height;
        }


        public bool IsInside(Actor actor)
        {
            var center = actor.Center;
            var ul = UpperLeft;
            var dx = center.X - ul.X;
            var dy = center.Y - ul.Y;
            return dx >= 0 && dx <= Width && dy >= 0 && dy <= Height;
        }

        /// <summary>
        /// Selects the contents of the rubber band.
        /// </summary>
        public void SelectContents()
        {
            foreach (var actor in canvas.Actors.All)
            {
                if (IsInside(actor))
                    canvas.Actors.Select(actor);
            }
        }

    }
}
